package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.WenjianXiuding;
import com.amsp.oa.oademo.entity.WenjianXiudingDetail;
import com.amsp.oa.oademo.mapper.WenjianXiudingMapper;
import com.amsp.oa.oademo.mapper.WenjianXiudingDetailMapper;
import com.amsp.oa.oademo.service.WenjianXiudingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WenjianXiudingServiceImpl extends ServiceImpl<WenjianXiudingMapper, WenjianXiuding> implements WenjianXiudingService {

    @Autowired
    private WenjianXiudingDetailMapper detailMapper;

    @Override
    public IPage<WenjianXiuding> getPage(Integer current, Integer size, String fileName, String fileNumber, String applyDepartment) {
        LambdaQueryWrapper<WenjianXiuding> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.isNotBlank(fileName)) {
            wrapper.like(WenjianXiuding::getFileName, fileName);
        }
        if (StringUtils.isNotBlank(fileNumber)) {
            wrapper.like(WenjianXiuding::getFileNumber, fileNumber);
        }
        if (StringUtils.isNotBlank(applyDepartment)) {
            wrapper.like(WenjianXiuding::getApplyDepartment, applyDepartment);
        }
        
        wrapper.orderByDesc(WenjianXiuding::getCreateTime);
        
        return page(new Page<>(current, size), wrapper);
    }

    @Override
    public WenjianXiuding getDetailById(Long id) {
        WenjianXiuding wenjianXiuding = getById(id);
        if (wenjianXiuding != null) {
            LambdaQueryWrapper<WenjianXiudingDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WenjianXiudingDetail::getXiudingId, id);
            List<WenjianXiudingDetail> details = detailMapper.selectList(wrapper);
            wenjianXiuding.setDetails(details);
        }
        return wenjianXiuding;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWithDetails(WenjianXiuding wenjianXiuding) {
        boolean result = saveOrUpdate(wenjianXiuding);
        if (result && wenjianXiuding.getDetails() != null) {
            // 删除原有明细
            LambdaQueryWrapper<WenjianXiudingDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WenjianXiudingDetail::getXiudingId, wenjianXiuding.getId());
            detailMapper.delete(wrapper);
            
            // 保存新明细
            for (WenjianXiudingDetail detail : wenjianXiuding.getDetails()) {
                detail.setXiudingId(wenjianXiuding.getId());
                detailMapper.insert(detail);
            }
        }
        return result;
    }
} 